package com.firefish.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;

/**
 * 功能: 描述.
 * 命令：
 * 1：查找所有目录下的md文件：find /Users/apple/Documents/Work/Development/gitee/article-list| grep -v '.idea' | grep -v 'buildPretty' | grep -v 'DS_Store' | grep 'md'
 * 2：查找md中的图片：egrep -Ra '!\[.*\]\(.*\)' `xxxxx` > /Users/apple/Documents/Work/Development/gitee/article-list/buildPretty/src/main/java/com/firefish/util/aaa.txt
 * 3:egrep -Ra '!\[.*\]\(.*\)' 'dir' | grep -v 'dir/.idea/' | grep -v 'dir/buildPretty' > /Users/apple/Documents/Work/Development/gitee/article-list/buildPretty/src/main/java/com/firefish/util/aaa.txt
 *
 * @author Fire Fish
 * <a href="https://gitee.com/firefish985">gitee个人主页</a>
 * <a href="https://github.com/firefish985">github个人主页</a>
 * @since 2023-08-12
 */
public class ListPngFile {
	public static void main(String[] args) throws Exception {
		String file = "/Users/apple/Documents/Work/Development/gitee/article-list/buildPretty/src/main/java/com/firefish/util/aaa.txt";
		List<String> strs = FileUtil.readLines(file, Charset.defaultCharset());

		List<String> result1 = FileUtil.listFileNames("/Users/apple/Documents/Work/aliyun-oss/dev-images");
		Set<String> result2 = new TreeSet<>();
		result2.addAll(listPngs(strs));

		// 本地文件与article-list的差(文章中那些png文件没有用到)
		result1.removeAll(result2);
		Collections.sort(result1);
		for (String s : result1) {
			System.out.println(s);
		}


//		// article-list与本地文件的差[如：有的图片直接从外部引入的]
//		List<String> result3 = FileUtil.listFileNames("/Users/apple/Documents/Work/aliyun-oss/dev-images");
//		Set<String> result4 = new TreeSet<>();
//		result4.addAll(listPngs(strs));
//
//
//		result4.removeAll(result3);
//		ArrayList<String> result5 = new ArrayList<>();
//		result5.addAll(result4);
//		Collections.sort(result5);
//		for (String s : result5) {
//			System.out.println(s);
//		}
	}

	private static Set<String> listPngs(List<String> strings) {
		HashMap<String, String> map = new HashMap<>();

		// 注意：使用正则的非贪心模式（防止一行出现多个匹配）
		Pattern pattern = Pattern.compile("!\\[.*?\\]\\(.*?\\)");
		for (String str : strings) {
			Matcher matcher = pattern.matcher(str);
			while (matcher.find()) {
				String group = matcher.group();// ![](/xxxx/xxxx/xxx.png)
				int index = group.lastIndexOf("/");
				String pngName = group.substring(index + 1, group.length() - 1);
				pngName = URLUtil.decode(pngName);
				String preValue = map.put(pngName, group);
				if (preValue != null && !preValue.equals(group)) {
					System.err.println("发生了文件覆盖异常，且全路径不同：" + preValue + " 和 " + group);
				}
			}
		}
		return map.keySet();
	}


}
